﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Logic.BaseContent;
using System.Data.SqlClient;

namespace Job.Web.userControls.Public
{
    public partial class BottomNews : BaseCtrl
    {
        BaseContentLogic contentLogic = new BaseContentLogic();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                BindHRList();
                BindManagerList();
                BindIndustryList();
            }
        }



        //绑定HR频道列表
        private void BindHRList()
        {
            const int RowCount = 5;
            DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyHRList, RowCount);

            if (ds != null)
            {
                this.hrList.DataSource = ds.Tables[0];
                this.hrList.DataBind();


                if (ds.Tables[0].Rows.Count >= RowCount)
                {
                    this.moreHR.NavigateUrl = WebCommon.GetApplicationPath(Request)
                        + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
                        + "&subjectid=" + ((int)Constants.SubjectType.CompanyHRList).ToString();
                    this.moreHR.Visible = true;
                }
                else
                {
                    this.moreHR.Visible = false;
                }
            }
            else
            {
                this.moreHR.Visible = false;
            }
        }

        //绑定行业动态列表
        private void BindIndustryList()
        {
            const int RowCount = 5;
            DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyIndustryList, RowCount);

            if (ds != null)
            {
                this.industryList.DataSource = ds.Tables[0];
                this.industryList.DataBind();

                if (ds.Tables[0].Rows.Count >= RowCount)
                {
                    this.moreIndustry.NavigateUrl = WebCommon.GetApplicationPath(Request)
                        + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
                        + "&subjectid=" + ((int)Constants.SubjectType.CompanyIndustryList).ToString();

                    this.moreIndustry.Visible = true;
                }
                else
                {
                    this.moreIndustry.Visible = false;
                }
            }
            else
            {
                this.moreIndustry.Visible = false;
            }
        }

        //绑定管理资讯列表
        private void BindManagerList()
        {
            const int RowCount = 5;
            DataSet ds = QueryTopList((int)Constants.SubjectType.CompanyManagerList, RowCount);

            if (ds != null)
            {
                this.managerList.DataSource = ds.Tables[0];
                this.managerList.DataBind();

                if (ds.Tables[0].Rows.Count >= RowCount)
                {
                    this.moreManager.NavigateUrl = WebCommon.GetApplicationPath(Request)
                        + "Item.aspx?categoryid=" + ((int)Constants.CategoryType.Company).ToString()
                        + "&subjectid=" + ((int)Constants.SubjectType.CompanyManagerList).ToString();
                    this.moreManager.Visible = true;
                }
                else
                {
                    this.moreManager.Visible = false;
                }
            }
            else
            {
                this.moreManager.Visible = false;
            }
        }


        //得到某个Subject所对应的前N条记录
        private DataSet QueryTopList(int subject, int number)
        {
            ArrayList param = new ArrayList();

            SqlParameter pm = new SqlParameter();
            pm.ParameterName = "subject_id";
            pm.Value = subject;
            pm.SqlDbType = SqlDbType.Int;
            param.Add(pm);

            DataSet ds = contentLogic.GetBaseContentList(number, 1, 0, "order_no {0},content_id {1}", Job.Web.Framework.SortDirection.Descending, param);
            return ds;
        }

        protected void hrList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink hrDetail = (HyperLink)e.Item.FindControl("hrLink");
                        Label lbContentId = (Label)e.Item.FindControl("hrId");
                        Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

                        BaseInfo hrInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

                        hrDetail.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(hrInfo.CatagoryId) + "&subjectid="
                            + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
                        if (hrDetail.Text.Length > 15)
                            hrDetail.Text = hrDetail.Text.Substring(0, 15) + "...";
                        break;
                    }
            }
        }

        protected void managerList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink managerDetail = (HyperLink)e.Item.FindControl("managerLink");
                        Label lbContentId = (Label)e.Item.FindControl("managerId");
                        Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

                        BaseInfo managerInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

                        managerDetail.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(managerInfo.CatagoryId) + "&subjectid="
                            + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
                        if (managerDetail.Text.Length > 15)
                            managerDetail.Text = managerDetail.Text.Substring(0, 15) + "...";
                        break;
                    }
            }
        }

        protected void industryList_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            ListItemType type = e.Item.ItemType;
            switch (type)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                    {
                        HyperLink industryDetail = (HyperLink)e.Item.FindControl("industryLink");
                        Label lbContentId = (Label)e.Item.FindControl("industryId");
                        Label lbSubjectId = (Label)e.Item.FindControl("subjectId");

                        BaseInfo industryInfo = WebCommon.GetSubjectInfo(lbSubjectId.Text);

                        industryDetail.NavigateUrl = WebCommon.GetApplicationPath(Request) + "Item.aspx?categoryid="
                            + Convert.ToString(industryInfo.CatagoryId) + "&subjectid="
                            + lbSubjectId.Text.Trim() + "&contentid=" + lbContentId.Text.Trim();
                        if (industryDetail.Text.Length > 15)
                            industryDetail.Text = industryDetail.Text.Substring(0, 15) + "...";
                        break;
                    }
            }

        }

    }
}